package com.lkd.handler;

import com.lkd.business.MsgHandler;
import com.lkd.config.TopicConfig;
import com.lkd.contract.SupplyTaskContract;
import com.lkd.emq.Topic;
import com.lkd.service.VendingMachineService;
import com.lkd.utils.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.io.IOException;

/**
 * @author brianxia
 * @version 1.0
 * @date 2022-12-02 15:14
 */
@Component
@Topic(TopicConfig.VMS_SUPPLY_TOPIC)
@Slf4j
public class TaskSupplyCompleteHandler implements MsgHandler {

    @Autowired
    private VendingMachineService vendingMachineService;

    @Override
    public void process(String jsonMsg) throws IOException {
        if(!StringUtils.isEmpty(jsonMsg)){
            SupplyTaskContract contract = JsonUtil.getByJson(jsonMsg, SupplyTaskContract.class);
            if(StringUtils.isEmpty(contract.getInnerCode()) ||
                    CollectionUtils.isEmpty(contract.getChannelList())){
                log.error("消息协议数据为空" + jsonMsg);
                return;
            }

            vendingMachineService.supplyChannel(contract);

        }
    }
}
